package com.fp.m7.client.presenter;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.place.PlaceRequestEvent;

import com.fp.m7.client.AccountContext;
import com.fp.m7.client.event.AccountAuthenticatedEvent;
import com.fp.m7.client.event.AccountAuthenticatedEventHandler;
import com.fp.m7.client.place.NewCommentPlace;
import com.fp.m7.client.place.ProfilePlace;
import com.fp.m7.client.place.SettingsPlace;
import com.fp.m7.client.place.TimelinePlace;
import com.fp.m7.shared.Constants;
import com.fp.m7.shared.dto.AccountDto;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;

/**
 * A header presenter for authorized user. Its display contains: New Comment, Profile, Timeline, Settings, Logout
 * (logout is a static link though) buttons.
 * 
 * @author 7
 */
public class HeaderSecuredPresenter extends HeaderPresenter<HeaderSecuredPresenter.Display> {

    public interface Display extends HeaderPresenter.Display {

        HasClickHandlers getNewCommentButton();

        HasClickHandlers getProfileButton();

        HasClickHandlers getTimelineButton();

        HasClickHandlers getSettingsButton();

        HasClickHandlers getLogoutButton();

        void setContent(String username);

    }

    private AccountContext accountContext;

    @Inject
    public HeaderSecuredPresenter(Display display, EventBus eventBus, AccountContext accountContext) {
        super(display, eventBus);

        this.accountContext = accountContext;
    }

    @Override
    protected void onBind() {
        // react on every account context change
        eventBus.addHandler(AccountAuthenticatedEvent.TYPE, new AccountAuthenticatedEventHandler() {
            @Override
            public void onAccountAuthenticated(AccountAuthenticatedEvent accountAuthenticatedEvent) {
                displayLoggedUsersNickname(accountAuthenticatedEvent.getAuthenticatedAccount());
            }
        });

        display.getNewCommentButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                PlaceRequest req = new PlaceRequest(NewCommentPlace.NAME);
                eventBus.fireEvent(new PlaceRequestEvent(req));
            }
        });

        display.getProfileButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                PlaceRequest req = new PlaceRequest(ProfilePlace.NAME);
                eventBus.fireEvent(new PlaceRequestEvent(req));
            }
        });

        display.getTimelineButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                PlaceRequest req = new PlaceRequest(TimelinePlace.NAME);
                eventBus.fireEvent(new PlaceRequestEvent(req));
            }
        });

        display.getSettingsButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                PlaceRequest req = new PlaceRequest(SettingsPlace.NAME);
                eventBus.fireEvent(new PlaceRequestEvent(req));
            }
        });

        display.getLogoutButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent paramClickEvent) {
                accountContext.setAuthenticatedAccount(null);
                Window.Location.replace(Constants.LOG_OUT_URL);
            }
        });
    }

    @Override
    protected void onUnbind() {
    }

    @Override
    protected void onRevealDisplay() {
        displayLoggedUsersNickname(accountContext.getAuthenticatedAccount());
    }

    private void displayLoggedUsersNickname(AccountDto account) {
        String username = "";
        if (account != null) {
            username = account.getUserDto().getNickname();
        }
        display.setContent(username);
    }
}
